﻿/*-----------------------------------------------
// File: CGKit.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

public class CGKit : MonoBehaviour
{
	public float videoDuration = 26f;
    //public UITweener mask;
    public bool isPlayVideo;
    void Start()
    {
        isNeedClose = false;
    }

	public float videoDeltaTime = 0f;

    public void ShowCG()
    {
        gameObject.SetActive(true);
        if (GameConfig.Instance.IsFirstBattleEnter)
        {
            Debug.Log("CG start");
#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
            AssetUpdateManager.Instance.PrepareTableData(EnterFirstBattle);
#else
            EnterFirstBattle();
#endif
            //#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
            //            Handheld.PlayFullScreenMovie("video.mp4", Color.black,
            //                FullScreenMovieControlMode.CancelOnInput, FullScreenMovieScalingMode.AspectFit);
            //            isPlayVideo = true;
            //			videoDeltaTime = 0f;
            //#else
            //            EnterFirstBattle();
            //#endif
        }
        else
        {
            Debug.Log("not first");
            ManagerController.Instance.ToLogin();
            GameObject.Destroy(gameObject);
        }
    }



    void EnterFirstBattle()
    {
        isPlayVideo = false;
        GameConfig.Instance.IsFirstBattleEnter = false;
        StaticData.isFirstEnterGame = false;
        ManagerController.Instance.LoginToBattle();
        FirstCheckPointUtil.CheckPoint(EnterGameCheckPointEnum.PlayAudioEnd);
    }

    bool isNeedClose = false;

    void Update()
    {
#if UNITY_IPHONE
		if (isPlayVideo){
			videoDeltaTime += Time.deltaTime;
			if (videoDeltaTime >= videoDuration){
				isPlayVideo = false;
				AssetUpdateManager.Instance.PrepareTableData(EnterFirstBattle);
			}

			if (Input.GetMouseButtonDown(0)){
				isPlayVideo = false;
				AssetUpdateManager.Instance.PrepareTableData(EnterFirstBattle);
			}

		}

#endif

        if (isNeedClose)
        {
            Destroy(gameObject);
            return;
        }
    }

    public void Close()
    {
        isNeedClose = true;
    }

#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
    public void OnApplicationPause(bool isPause)
    {
        if(isPlayVideo && !isPause)
        {
            AssetUpdateManager.Instance.PrepareTableData(EnterFirstBattle);
        }
    }
#endif
}